<?php 
namespace Phpcmf\Controllers;


class Huodong extends \Phpcmf\Home\Module
{
    
    public function jiance() {
		$id = (int)\Phpcmf\Service::L('input')->get('id');
		if (!$this->uid) {
		    $rt=[
		    	'code'=>0,
		        'msg'=>'没有登陆',
		    	'data'=>'',
		    ];
		    //dr_return_data($rt['code'], $rt['msg'], $rt['data']);
			$this->_msg($rt['code'], $rt['msg'], $rt['data']);
		} elseif (!$id) {
			$rt=[
				'code'=>0,
			    'msg'=>'id参数不完整',
				'data'=>'',
			];
			$this->_msg($rt['code'], $rt['msg'], $rt['data']);
		}
        // 获取帖子数据
		$table = '1_quan_form_son';
		$tiedata =\Phpcmf\Service::M()->db->table($table)->where('id', $id)->where('hdzt',1)->get()->getRowArray();
		
		$futiedata =\Phpcmf\Service::M()->db->table($table.'_data_0')->where('id', $id)->get()->getRowArray();
		if (!$tiedata) {
		    $rt=[
		    	'code'=>0,
		        'msg'=>'没有这个商品',
		    	'data'=>'',
		    ];
		    $this->_msg($rt['code'], $rt['msg'], $rt['data']);
		} 
		
        $dataid=[
            'id'=> $tiedata['id'], //九格图
        ];

        // seo
		$datas = dr_string2array($tiedata);
		$fudata = dr_string2array($futiedata);
		//my-插件目录_模型名称-相关ID-数量-SKU参数值 （数量-SKU参数值可以忽略）
        \Phpcmf\Service::V()->assign(\Phpcmf\Service::L('Seo')->get_seo_value($datas, [
			'payform' => dr_payform("my-quan_shangpin-".$id),
			'datas' => $datas,
			'dataf' => $fudata,
            'meta_title' => isset($this->form['setting']['seo']['title']) && $this->form['setting']['seo']['title'] ? $this->form['setting']['seo']['title'] : $data['title'].SITE_SEOJOIN.dr_lang($this->form['name']),
            'meta_keywords' => isset($this->form['setting']['seo']['keywords']) && $this->form['setting']['seo']['keywords'] ? $this->form['setting']['seo']['keywords'] : $data['title'].SITE_SEOJOIN.dr_lang($this->form['name']),
            'meta_description' => isset($this->form['setting']['seo']['description']) && $this->form['setting']['seo']['description'] ? $this->form['setting']['seo']['description'] : $data['title'].SITE_SEOJOIN.dr_lang($this->form['name']),
        ]));
        
        \Phpcmf\Service::V()->display("mform_show-huodong-buy.html");
		
    }
	public function zhifu() {
		$id = (int)\Phpcmf\Service::L('input')->get('id');
		$table = '1_quan_dizhi';
		$sku = \Phpcmf\Service::M()->db->table($table)->where('uid', $this->uid)->where('moren',1)->get()->getRowArray();
		//$skus = dr_string2array($sku);
		$data =dr_payform("my-quan_huodong-".$id."-1",);
		$rt=[
			'code'=>1,
		    'msg'=>'支付页面',
			'data'=>$data,
		];
		
		return $this->_json($rt['code'], $rt['msg'], $rt['data']);
	}
	public function buy() {
		$id = (int)\Phpcmf\Service::L('input')->get('id');
		if (!$this->uid) {
		    $rt=[
		    	'code'=>0,
		        'msg'=>'没有登陆',
		    	'data'=>'',
		    ];
		    return $this->_json($rt['code'], $rt['msg'], $rt['data']);
		} elseif (!$id) {
		   
			$rt=[
				'code'=>0,
			    'msg'=>'id参数不完整',
				'data'=>'',
			];
			return $this->_json($rt['code'], $rt['msg'], $rt['data']);
		}
		// 获取帖子数据
		$table = '1_quan_form_son';
		$tiedata =\Phpcmf\Service::M()->db->table($table)->where('id', $id)->where('is_sale',1)->get()->getRowArray();
		if (!$tiedata) {
		    $rt=[
		    	'code'=>0,
		        'msg'=>'没有这个商品',
		    	'data'=>'',
		    ];
		    return $this->_json($rt['code'], $rt['msg'], $rt['data']);
		} 
		$dataid=[
		    'id'=> $tiedata['id'], //九格图
		];
		$rt=[
			'code'=>1,
		    'msg'=>'购物成功',
			'data'=>$dataid,
		];
		return $this->_json($rt['code'], $rt['msg'], $rt['data']);
	}
	//卖家
	public function tomaijialisthtml() {
		$datas='';
		\Phpcmf\Service::V()->assign(\Phpcmf\Service::L('Seo')->get_seo_value($datas, [
		    'meta_title' => isset($this->form['setting']['seo']['title']) && $this->form['setting']['seo']['title'] ? $this->form['setting']['seo']['title'] : $data['title'].SITE_SEOJOIN.dr_lang($this->form['name']),
		    'meta_keywords' => isset($this->form['setting']['seo']['keywords']) && $this->form['setting']['seo']['keywords'] ? $this->form['setting']['seo']['keywords'] : $data['title'].SITE_SEOJOIN.dr_lang($this->form['name']),
		    'meta_description' => isset($this->form['setting']['seo']['description']) && $this->form['setting']['seo']['description'] ? $this->form['setting']['seo']['description'] : $data['title'].SITE_SEOJOIN.dr_lang($this->form['name']),
		]));
		
		\Phpcmf\Service::V()->display("mform_show-shangpin-tomaijia.html");
	}
	//卖家
	public function tomaijialist() {
		$id = (int)\Phpcmf\Service::L('input')->get('id');
		// 获取帖子数据
		$tietable = '1_quan_form_son';
		$tiedata =\Phpcmf\Service::M()->db->table($tietable)->where('id', $id)->where('uid',$this->uid)->get()->getRowArray();
		
		if($tiedata['uid'] != $this->uid){
			$rt=[
				'code'=>0,
			    'msg'=>'无权查看',
				'data'=>'',
			];
			return $this->_json($rt['code'], $rt['msg'], $rt['data']);
		}
		$table = '1_quan_gouwu_buy';
		$maijiadata =\Phpcmf\Service::M()->table($table)->where('touid', $this->uid)->where('cid', $id)->order_by('inputtime desc, id desc')->getAll();
		if(!$maijiadata){
			$rt=[
				'code'=>0,
			    'msg'=>'没有订单',
				'data'=>'',
			];
			return $this->_json($rt['code'], $rt['msg'], $rt['data']);
		}
		$items = [];
		//$maijiadata['inputtime'] = dr_date($maijiadata['inputtime'], 'Y-m-d-H-i-s');
		foreach ($maijiadata as $i => $item) {  
		    if (isset($item['inputtime'])) {  
		        $item['time'] = dr_date($item['inputtime'], 'Y-m-d-H:i:s');
				$item['inputtimey'] = dr_date($item['inputtime'], "Y-m-d");
				$item['inputtimes'] = dr_date($item['inputtime'], "H:i:s");
				$item['jinxingy'] = dr_date($item['jinxingtime'], "Y-m-d");
				$item['jinxings'] = dr_date($item['jinxingtime'], "H:i:s");
				$item['paiqiy'] = dr_date($item['paiqitime'], "Y-m-d");
				$item['paiqis'] = dr_date($item['paiqitime'], "H:i:s");
				$item['wanchengy'] = dr_date($item['wanchengtime'], "Y-m-d");
				$item['wanchengs'] = dr_date($item['wanchengtime'], "H:i:s");
		    }  
			$item['uid'] = dr_member_info($item['uid']);
			$item['touid'] = dr_member_info($item['touid']);
			$items[$i]=$item;
		}
		$data =[
			'mai'=>$items,
			'tie'=>$tiedata
		];
		$rt=[
			'code'=>1,
		    'msg'=>'获取成功',
			'data'=>$data,
		];
		return $this->_json($rt['code'], $rt['msg'], $rt['data']);
	}
	//买家
	public function maijialisthtml() {
		$datas='';
		\Phpcmf\Service::V()->assign(
		\Phpcmf\Service::L('Seo')->get_seo_value($datas, [

		    'meta_title' => isset($this->form['setting']['seo']['title']) && $this->form['setting']['seo']['title'] ? $this->form['setting']['seo']['title'] : $data['title'].SITE_SEOJOIN.dr_lang($this->form['name']),
		    'meta_keywords' => isset($this->form['setting']['seo']['keywords']) && $this->form['setting']['seo']['keywords'] ? $this->form['setting']['seo']['keywords'] : $data['title'].SITE_SEOJOIN.dr_lang($this->form['name']),
		    'meta_description' => isset($this->form['setting']['seo']['description']) && $this->form['setting']['seo']['description'] ? $this->form['setting']['seo']['description'] : $data['title'].SITE_SEOJOIN.dr_lang($this->form['name']),
		]));
		
		\Phpcmf\Service::V()->display("mform_show-shangpin-maijia.html");
	}
	//买家
	public function maijialist() {
		
		$id = (int)\Phpcmf\Service::L('input')->get('id');
		// 获取帖子数据
		$tietable = '1_quan_form_son';
		$tiedata =\Phpcmf\Service::M()->db->table($tietable)->where('id', $id)->get()->getRowArray();
		
		$table = '1_quan_gouwu_buy';
		$maijiadata =\Phpcmf\Service::M()->table($table)->where('uid', $this->uid)->where('cid', $id)->order_by('inputtime desc, id desc')->getAll();
		if(!$maijiadata){
			$rt=[
				'code'=>0,
			    'msg'=>'没有订单',
				'data'=>'',
			];
			return $this->_json($rt['code'], $rt['msg'], $rt['data']);
		}
		$items = [];
		//$maijiadata['inputtime'] = dr_date($maijiadata['inputtime'], 'Y-m-d-H-i-s');
		foreach ($maijiadata as $i => $item) {  
		    if (isset($item['inputtime'])) {  
		        $item['time'] = dr_date($item['inputtime'], 'Y-m-d-H:i:s');
		    }
			$item['inputtimey'] = dr_date($item['inputtime'], "Y-m-d");
			$item['inputtimes'] = dr_date($item['inputtime'], "H:i:s");
			$item['jinxingy'] = dr_date($item['jinxingtime'], "Y-m-d");
			$item['jinxings'] = dr_date($item['jinxingtime'], "H:i:s");
			$item['paiqiy'] = dr_date($item['paiqitime'], "Y-m-d");
			$item['paiqis'] = dr_date($item['paiqitime'], "H:i:s");
			$item['wanchengy'] = dr_date($item['wanchengtime'], "Y-m-d");
			$item['wanchengs'] = dr_date($item['wanchengtime'], "H:i:s");
			$item['uid'] = dr_member_info($item['uid']);
			$item['touid'] = dr_member_info($item['touid']);
			$items[$i]=$item;
		}
		$data =[
			'mai'=>$items,
			'tie'=>$tiedata
		];
		$rt=[
			'code'=>1,
		    'msg'=>'获取成功',
			'data'=>$data,
		];
		return $this->_json($rt['code'], $rt['msg'], $rt['data']);
	}
	// 卖家发货
	public function maijiafahuo() {
		
		if(IS_POST){
			$id = (int)\Phpcmf\Service::L('input')->get('id');
			
			$danhao = dr_safe_replace($_POST['danhao']);
			$wuliu = dr_safe_replace($_POST['wuliu']);
			$table = '1_quan_gouwu_buy';
			$tiedata =\Phpcmf\Service::M()->db->table($table)->where('id', $id)->where('touid',$this->uid)->get()->getRowArray();
			
			if(!$tiedata){
				$rt=[
					'code'=>0,
					'msg'=>'没权操作',
					'data'=>'',
				];
				return $this->_json($rt['code'], $rt['msg'], $rt['data']);
			}
			\Phpcmf\Service::M()->db->table($table)->where('touid', $this->uid)->where('id', $id)->update([
				'danhao' => $danhao,
				'wuliu' => $wuliu,
				'fahuozhuangtai' => 1, //发送中
			]);
			$rt=[
				'code'=>1,
				'msg'=>'修改成功',
				'data'=>$data,
			];
			return $this->_json($rt['code'], $rt['msg'], $rt['data']);
		} else {
			$id = (int)\Phpcmf\Service::L('input')->get('id');
			$table = '1_quan_gouwu_buy';
			$tiedata =\Phpcmf\Service::M()->db->table($table)->where('id', $id)->where('touid',$this->uid)->get()->getRowArray();
			if(!$tiedata){
				$rt=[
					'code'=>0,
					'msg'=>'没权操作',
					'data'=>'',
				];
				return $this->_json($rt['code'], $rt['msg'], $rt['data']);
			}
			$rt=[
				'code'=>1,
				'msg'=>'获取成功',
				'data'=>$tiedata,
			];
			return $this->_json($rt['code'], $rt['msg'], $rt['data']);
		}
	}
	// 卖家发货
	public function maijiafuwu() {
		if(IS_POST){
			$id = dr_safe_replace($_POST['id']);
			$dingdanzhuangtai = (int)dr_safe_replace($_POST['zt']);
			$ztid = (int)dr_safe_replace($_POST['ztid']);
			dr_safe_replace($_POST['id']);
			$table = '1_quan_gouwu_buy';
			$tiedata =\Phpcmf\Service::M()->db->table($table)->where('id', $id)->where('touid',$this->uid)->get()->getRowArray();
			
			if(!$tiedata){
				$rt=[
					'code'=>0,
					'msg'=>'没权操作',
					'data'=>'',
				];
				return $this->_json($rt['code'], $rt['msg'], $rt['data']);
			}
			if($dingdanzhuangtai=='1'){
				if($ztid == '1'){
					\Phpcmf\Service::M()->db->table($table)->where('touid', $this->uid)->where('id', $id)->update([
						'paiqitime' => SYS_TIME,
						'dingdanzhuangtai' => 2, 
					]);
					$rt=[
						'code'=>1,
						'msg'=>'排期成功',
						'data'=>'',
					];
					return $this->_json($rt['code'], $rt['msg'], $rt['data']);
				}
				
			} 
			if($dingdanzhuangtai=='2' && $ztid=='2'){
				\Phpcmf\Service::M()->db->table($table)->where('touid', $this->uid)->where('id', $id)->update([
					'jinxingtime' => SYS_TIME,
					'dingdanzhuangtai' => 3, 
				]);
				$rt=[
					'code'=>1,
					'msg'=>'进行成功',
					'data'=>'',
				];
				return $this->_json($rt['code'], $rt['msg'], $rt['data']);
			}
			if($dingdanzhuangtai=='3' && $ztid=='3'){
				\Phpcmf\Service::M()->db->table($table)->where('touid', $this->uid)->where('id', $id)->update([
					'wanchengtime' => SYS_TIME,
					'dingdanzhuangtai' => 4, 
				]);
				$rt=[
					'code'=>1,
					'msg'=>'服务成功',
					'data'=>'',
				];
				return $this->_json($rt['code'], $rt['msg'], $rt['data']);
			}
			if($dingdanzhuangtai=='5' && $ztid=='1'){
				\Phpcmf\Service::M()->db->table($table)->where('touid', $this->uid)->where('id', $id)->update([
					'wanchengtime' => SYS_TIME,
					'dingdanzhuangtai' => 4, 
				]);
				$rt=[
					'code'=>1,
					'msg'=>'服务成功',
					'data'=>'',
				];
				return $this->_json($rt['code'], $rt['msg'], $rt['data']);
			}
			if($ztid==4){
				$rt=[
					'code'=>1,
					'msg'=>'已经完成',
					'data'=>'',
				];
				return $this->_json($rt['code'], $rt['msg'], $rt['data']);
			} else {
				$rt=[
					'code'=>0,
					'msg'=>'请根据时间排序',
					'data'=>'',
				];
				return $this->_json($rt['code'], $rt['msg'], $rt['data']);
			}
			
			
		}
	}
	
	
	
	
	
    
	
}